package com.hospital.tools;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.view.*;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.hospital.activity.R;

import java.util.Calendar;
import java.util.UUID;

/**
 * Created by wangjun on 2015/5/26.
 */
public class Util{
    private final static String DEBUG_TAG = "kingt_debug";

    /**
     * 点击页面空白出
     *
     * @param context
     */
    public static void softInputTouchListener(final Context context) {
        ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                return false;
            }
        });
    }

    /**
     * 点击页面空白出
     *
     * @param context
     * @param scrollView
     */
    public static void softInputTouchListener(final Context context, final ScrollView scrollView) {
        scrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final InputMethodManager imm1 = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm1.hideSoftInputFromWindow(scrollView.getWindowToken(), 0);
                return false;
            }
        });
    }

    /**
     * 隐藏键盘
     *
     * @param view
     * @param context
     */
    public static void hideSoftInput(View view, Context context) {
        final InputMethodManager imm1 = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm1.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


    /**
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static String formatDateToString(int year, int month, int day) {
        return year + "-" + month + "-" + day + "";
    }

    /**
     * @param activity
     * @param content
     */
    public static void showToast(final Activity activity, final String content) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast toast = Toast.makeText(activity, content, Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }

    /**
     * 获取Android设备的唯一识别码｜设备号｜序号｜UUID
     * help:http://www.cnblogs.com/xiaowenji/archive/2011/01/11/1933087.html
     *
     * @param context
     * @return
     */
    public static String getDeviceUid(Context context) {
        final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        final String tmDevice, tmSerial, androidId;
        tmDevice = "" + tm.getDeviceId();
        tmSerial = "" + tm.getSimSerialNumber();
        androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
        UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
        return deviceUuid.toString();
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 确认Dialog
     *
     * @param context
     * @return
     */
    public static Dialog publishConfirm(final Context context, final Activity activity) {

        final Dialog dialog = new Dialog(context, R.style.dialog);
        final View v = LayoutInflater.from(context).inflate(R.layout.dialog_confirm, null);
        Button next = (Button) v.findViewById(R.id.next);
        final Button cancel = (Button) v.findViewById(R.id.cancel);
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                //	Util.showToast((Activity) context, "next");
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.onBackPressed();
                //	Util.showToast((Activity) context, "cancel");
            }
        });
        dialog.setContentView(v);
        dialog.show();
//      set width after show
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = dialog.getWindow();
        lp.copyFrom(window.getAttributes());
//      lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.width = dip2px(context, 250);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        return dialog;
    }


}

